<?php
/**
 * Pertyler
 * Table Browser Page
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pertyler;

require '../library/Pert/General/Link.php';

require '../library/Pert/General/Page.php';

require '../databaseModel/Table.php';

require '../databaseModel/Role.php';

require '../control/main.php';

if(!($moderator = Role_Moderator::Select()))
	if(require '../control/Login.php')
		return;

require 'KoolControls/KoolAjax/koolajax.php';

require 'KoolControls/KoolTreeView/kooltreeview.php';

$treeView = new \KoolTreeView('treeView');

$treeView->imageFolder = $iconsFolder;

$treeView->styleFolder = 'default/';

$treeView->showLines = true;

$root = $treeView->getRootNode();

$root->text = Name;

$root->expand = true;

$root->image = 'ada.gif';

$root->showPlusMinus = false;

function addTable(Table $table)
{
	global $treeView;

	if(file_exists($treeView->imageFolder . $table->Schema() . '.gif'))
		$icon = $table->Schema() . '.gif';
	else
		$icon = 'public.gif';

	if($table->SelectPrivilegeArray())
	{
		if($table->FileTable())
			$link = new \Pert\Link('files.php',
					'body');
		else
			$link = new \Pert\Link('Table.php',
					'body');

		$link->Table = $table->Id();

		$treeView->Add($table->SelectParentTable() ? $table->SelectParentTable()->UniqueName() : 'root',
				$table->UniqueName(),
				$link->HTML($table),
				$table->SelectParentTable() ? true : false,
				$icon);
	}
	else
		$treeView->Add($table->SelectParentTable() ? $table->SelectParentTable()->UniqueName() : 'root',
				$table->UniqueName(),
				$table,
				$table->SelectParentTable() ? true : false,
				$icon);

	foreach($table->SelectTableArray() AS $aTable)
		addTable($aTable);
}

foreach(Table::SelectTopLevelTableArray() AS $table)
	addTable($table);

echo \Pert\Page::Get()->HeaderHTML() . $treeView->Render() . \Pert\Page::Get()->FooterHTML();
